import { action, observable } from 'mobx';
import {
  listMenuTree,
  saveModule,
  deleteModule,
  listApp,
  sort, listModuleRes,
} from '../service';
import loading from '../../../components/Loading';
import { message } from 'antd';
import React from 'react';

class AppStore {

  // 功能菜单树
  @observable moduleTreeData = {
    dataSource: [],
    loading: false,
    expandedKeys: [],
  };

  // 功能菜单抽屉
  @observable moduleDrawerData = {
    loading: false,
    visible: false,
    editData: {},
    resList: [],
  };

  // 功能菜单树抽屉
  @observable moduleTreeDrawerData = {
    editData: {},
    visible: false,
    loading: false,
    expandedKeys: [],
  };

  // 系统字典
  @observable appList = [];

  /**
   * 析构函数
   */
  constructor() {
    this.initData();
  }

  /**
   * 初始化页面，加载数据
   * @returns {Promise<void>}
   */
  async initData() {
    this.appList = await listApp();
    await this.initModuleTreeData();

    let expandedKeys = ["0"], expandedTreeKeys = [];
    const loop = data => {
      data.map(item => {
        // if (item.cascade.split('.').length <= 1) {
        //   expandedKeys.push(item.id);
        // }
        expandedTreeKeys.push(item.id);
        if (item.children && item.children.length > 0) {
          loop(item.children);
        }
      });
    };
    loop(this.moduleTreeData.dataSource);
    this.moduleTreeData.expandedKeys = expandedKeys;
    this.moduleTreeDrawerData.expandedKeys = expandedTreeKeys;
  }

  /**
   * 初始化功能菜单列表数据
   * @returns {Promise<void>}
   */
  async initModuleTreeData() {
    this.moduleTreeData.dataSource = await loading(listMenuTree, this.moduleTreeData)();

  }

  /**
   * 展开列表
   * @param expanded 是否展开
   * @param record 展开的行
   */
  @action
  expandRow(expanded, record) {
    let pos = -1;
    this.moduleTreeData.expandedKeys.map(((value, index) => {
      if (value == record.id) pos = index;
    }));
    let expandedKeys = this.moduleTreeData.expandedKeys;
    expanded ? expandedKeys.push(record.id) : expandedKeys.splice(pos, 1);

    this.moduleTreeData = { ...this.moduleTreeData, expandedKeys: expandedKeys };
  }

  /**
   * 展开树
   * @param expandedKeys 展开的树节点列表
   */
  @action
  expandTree(expandedKeys) {
    this.moduleTreeDrawerData = { ...this.moduleTreeDrawerData, expandedKeys: expandedKeys };
  }


  /**
   * 新增或者修改功能菜单
   * @param record 功能菜单
   */
  async showModuleDrawer(record) {
    let moduleResList = await listModuleRes(record.id);
    // 弹出新增功能模块抽屉
    this.moduleDrawerData = {
      ...this.moduleDrawerData,
      resList: moduleResList,
      editData: { ...record },
      visible: true,
    };
  }

  /**
   * 关闭功能菜单抽屉
   */
  @action
  closeModuleDrawer() {
    this.moduleDrawerData = { ...this.moduleDrawerData, visible: false };
  };

  /**
   * 保存功能菜单数据，并关闭抽屉
   * @param module
   * @returns {Promise<void>}
   */
  @action
  async saveModule(module) {
    module.resList = this.moduleDrawerData.resList;
    await loading(saveModule, this.moduleDrawerData)(module);
    this.moduleDrawerData = { ...this.moduleDrawerData, visible: false };
    this.initModuleTreeData();
    message.success('保存资源信息成功！');
  }

  /**
   * 删除资源响应函数
   * @param record 资源
   */
  @action
  async deleteModule(record) {
    await deleteModule(record.id);
    this.initModuleTreeData();
  }

  @action
  addRes() {
    let resList = this.moduleDrawerData.resList;
    resList.push({});
    this.moduleDrawerData = { ...this.moduleDrawerData, resList: resList };
  }

  @action
  inputRes(col, value, index) {
    let resList = this.moduleDrawerData.resList;
    let record = resList[index];
    record[col] = value;
    resList.splice(index, 1, record);
    this.moduleDrawerData = { ...this.moduleDrawerData, resList: resList };
  }

  @action
  removeRes(index) {
    console.log('index:', index, this.moduleDrawerData.resList);
    let resList = this.moduleDrawerData.resList;
    resList.splice(index, 1);
    this.moduleDrawerData = { ...this.moduleDrawerData, resList: resList };
  }

  @action
  showModuleTreeDrawer(record) {
    // 弹出新增资源抽屉
    this.moduleTreeDrawerData = { ...this.moduleTreeDrawerData, editData: record, visible: true };
  }

  @action
  closeModuleTreeDrawer() {
    this.moduleTreeDrawerData = { ...this.moduleTreeDrawerData, visible: false };
  }

  @action
  async sort(params) {
    await sort({ ...params, srcId: this.moduleTreeDrawerData.editData.id });
    await this.initModuleTreeData();
    this.closeModuleTreeDrawer();
  }
}

export default AppStore;
